package com.fh.test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fh.mapper.ProductMapper;
import com.fh.model.ProductSku;
import com.fh.model.es.ProductEs;
import com.fh.model.vo.EsVO;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@SpringBootTest
public class importProductEs {

@Resource
private ProductMapper productMapper;
@Resource
private RestHighLevelClient restHighLevelClient;
@Resource
private ObjectMapper objectMapper;


    @Test
    void  importData() throws IOException {

        List<EsVO> esVOS = productMapper.selectEsVO();

        for (int i = 0; i < esVOS.size(); i++) {
            EsVO esVO=esVOS.get(i);
            ProductEs productEs=new ProductEs();
            productEs.setProductId(esVO.getProductId());
            productEs.setProductName(esVO.getProductName());
            productEs.setSoldNum(esVO.getSoldNum());

            List<ProductSku> productSku=esVO.getSkuList();
            int SellPrice=productSku.size()==0?0:productSku.get(0).getSellPrice();
            String SkuImg=productSku.size()==0?"":productSku.get(0).getSkuImg();
            productEs.setSellPrice(SellPrice);
            productEs.setSkuImg(SkuImg);


            IndexRequest indexRequest=new IndexRequest("fengmi");
            indexRequest.id(productEs.getProductId());
            indexRequest.source(objectMapper.writeValueAsString(productEs), XContentType.JSON);
            restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);



        }


    }

}
